package com.yixia.common.core.domain.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yixia.common.annotation.Excel;
import com.yixia.common.annotation.Excel.ColumnType;
import com.yixia.common.annotation.Excel.Type;
import com.yixia.common.annotation.Excels;
import com.yixia.common.core.domain.BaseEntity;
import com.yixia.common.xss.Xss;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 *
 * 用户类
 */
@EqualsAndHashCode(callSuper = true)
@TableName(value ="sys_user")
@Data
public class SysUser extends BaseEntity {
    /**
     * 用户ID
     */
    @Excel(name = "用户序号(ID)", cellType = ColumnType.NUMERIC, prompt = "用户编号")
    @TableId(value = "user_id", type = IdType.AUTO)
    private Long userId;


    /**
     * 所属部门
     */
    @Excel(name = "部门编号", type = Type.IMPORT)
    @TableField(value = "dept_id")
    private Long deptId;

    /**
     * 用户名
     */
    @Excel(name = "用户名称")
    @Xss(message = "用户账号不能包含脚本字符")
    @NotBlank(message = "用户账号不能为空")
    @Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符")
    @TableField(value = "username")
    private String username;

    /**
     * 昵称
     */
    @Excel(name = "用户昵称")
    @Xss(message = "用户昵称不能包含脚本字符")
    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
    @TableField(value = "nick_name")
    private String nickName;

    /**
     * 密码
     */
    @TableField(value = "password")
    private String password;



    /**
     * 用户头像
     */
    @TableField(value = "avatar")
    private String avatar;

    /**
     * 用户邮箱
     */
    @Excel(name = "用户邮箱")
    @Email(message = "邮箱格式不正确")
    @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
    @TableField(value = "email")
    private String email;

    /**
     * 手机号码
     */
    @Excel(name = "手机号码")
    @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
    @TableField(value = "phonenumber")
    private String phonenumber;

    /**
     * 最后登录时间
     */
    @Excel(name = "最后登录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Type.EXPORT)
    @TableField(value = "login_date")
    private Date loginDate;

    /**
     * 帐号状态（0正常 1停用）
     */
    @Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
    @TableField(value = "status")
    private String status;


    /** 最后登录IP */
    @TableField(value = "login_ip")
    @Excel(name = "最后登录IP", type = Type.EXPORT)
    private String loginIp;


    /**
     * 用户性别
     */
    @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
    @TableField(value = "sex")
    private String sex;

    /**
     * 删除标志（0代表存在 2代表删除）
     */
    @TableField(value = "del_flag")
    private String delFlag;



    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**所属角色,多个角色逗号隔开*/
    @TableField(exist = false)
    private String roles;


    /**所属角色,多个角色*/
    @TableField(exist = false)
    private List<SysRole> roleList;


    /**`
     * 确认新密码
     */
    @TableField(exist = false)
    private String newPassword;

    /**
     * 旧密码（前端传来的）
     */
    @TableField(exist = false)
    private String oldPassword;


    /**
     * 角色ID（前端传来的，查询时使用）
     */
    @TableField(exist = false)
    private Long roleId;

    /** 部门对象 */
    @Excels({
            @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
            @Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
    })
    @TableField(exist = false)
    private SysDept dept;


    /**
     * 角色IDS（前端传来的，添加时使用）
     */
    @TableField(exist = false)
    private Long[] roleIds;
    /**
     * 岗位IDS（前端传来的，添加时使用）
     */
    @TableField(exist = false)
    private Long[] postIds;

    @TableField(exist = false)
    private Set<Integer> dataScope;


    @TableField(exist = false)
    private static final long serialVersionUID = 1L;


    public boolean isAdmin()
    {
        return isAdmin(this.userId);
    }

    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1L == userId;
    }



    public Long getUserId(){
        return this.userId;
    }


    /*构造方法*/
    public SysUser(Long userId) {
        this.userId = userId;
    }
    public SysUser(){}


}